﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Windows.Controls;
using BaocaoNPC.common;
using Component;
using CMISLibrary;
//using busBaoCao;
using CMISLibrary.Libraries;
using System.IO;
using Microsoft.VisualBasic;
using BaocaoNPC.Usercontrols.reports;

namespace BaocaoNPC.Usercontrols
{
    /// <summary>
    /// Interaction logic for INBAOCAO.xaml
    /// </summary>
    public partial class INBAOCAO : UserControl
    {
        #region Variables
        bus_TienichNPC.cls_TienichNPC_bus bus = new bus_TienichNPC.cls_TienichNPC_bus();
        //bus_TienichNPC.cls_ImportExcel_bus cls = new bus_TienichNPC.cls_ImportExcel_bus();
        public String strMadviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqly != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqly != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqly : "PA";
        public string strUsername = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "NPC_ATM";
        public String strTen_dviqly = NPCUS_DanhMuc.Pglobal.strTen_dviqly != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly : "cty..";
        public String strTen_dviqly_CT = NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT : "cap tren";
        public Int16 i16ThangHT = 0;
        public Int16 i16NamHT = 0;
        public int daysInMonth_HT = 1;
        private int DS = 0, IN = 0;
        string maloi="";
        int tt = 0;
        DateTime? ngaynop = DateTime.Now.Date;
        #endregion
        public INBAOCAO()
        {
            InitializeComponent();
            Component.TabIndexManager.RegisterTabManagerWithSelectAll(this);
            GET_THANGNAM_HT();
            GetD_DVI_QLY();
            dtpNgayNop.SelectedDate = DateTime.Now.Date; 
            daysInMonth_HT = System.DateTime.DaysInMonth(i16NamHT, i16ThangHT);
            dtpdenngay.SelectedDate = DateTime.Now.Date;
            dtptungay.SelectedDate = DateTime.Now.Date;
            dtpThangHTtim.Text = "0" + i16ThangHT.ToString() + "/" + i16NamHT.ToString();
            dtpNgayNop.IsEnabled = false;
            
            //RadDsachKhTonCuoithang.IsVisible = false;
            RadDsachKhTonCuoithang.IsEnabled = false;
            //dtptungay.Text = "01/" + i16ThangHT.ToString() + "/" + i16NamHT.ToString();
            //dtpdenngay.Text = daysInMonth_HT.ToString() + "/" + i16ThangHT.ToString() + "/" + i16NamHT.ToString();
        }
        private void GET_THANGNAM_HT()
        {
            try
            {
                DataSet dsthanglviec = new DataSet();
                //bus_TienichNPC.cls_TienichNPC_bus bus = new cls_TienichNPC_bus();
                //dsthanglviec = bus.get_thang_lviec();
                dsthanglviec = GlobalServices.Service_NPCUS.get_thang_lviec();
                if (dsthanglviec != null && dsthanglviec.Tables.Count > 0)
                {
                    foreach (DataRow dr in dsthanglviec.Tables[0].Rows)
                        if (dr["PHAN_HE"].ToString() == "CN" && dr["MA_DVIQLY"].ToString() == strMadviqly)
                        {
                            i16ThangHT = Convert.ToInt16(dr["THANG"]);
                            i16NamHT = Convert.ToInt16(dr["NAM"]);
                        }
                }
            }
            catch
            {
                i16ThangHT = Convert.ToInt16(DateTime.Now.Month);
                i16NamHT = Convert.ToInt16(DateTime.Now.Year);
            }
        }
        private void GetD_DVI_QLY()
        {
            try
            {
                DataSet dsD_DVI_QLY = new DataSet();

                //cls_TienichNPC_bus cls = new cls_TienichNPC_bus();

                //dsD_DVI_QLY = cls.select_D_DVI_QLY_ALL(strMa_dviqly);

                dsD_DVI_QLY = GlobalServices.Service_NPCUS.select_D_DVI_QLY_ALL(strMadviqly);

                if (dsD_DVI_QLY == null)
                {
                    System.Windows.MessageBox.Show("Không lấy được danh mục đơn vị quản lý.");
                }
                else
                {
                    DataView dv = dsD_DVI_QLY.Tables["D_DVI_QLY"].DefaultView;
                    cboDonVi.ItemsSource = dv;
                    cboDonVi.DisplayMemberPath = "TEN_DVIQLY";
                    cboDonVi.SelectedValuePath = "MA_DVIQLY";
                    cboDonVi.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }

        public void showErrorMess(string s)
        {
            Component.Validate.ValidationManager.showMessOnView(this, 5000, s, Component.Validate.ValidationManager.MESSEGER_TYPE.ERROR_MESSAGE);
        }
        public void showSucrMess(string s)
        {
            Component.Validate.ValidationManager.showMessOnView(this, 5000, s, Component.Validate.ValidationManager.MESSEGER_TYPE.SUCCESS_MESSAGE);
        }
        private void get_DS()
        {
            if (RadALLDSach.IsChecked == true)
            {
                DS = 0;
                radInChotNgay.IsEnabled = true;
                radIntheoNgay.IsEnabled = true;
                radInLai.IsEnabled = true;
                get_IN();
            }
            if (RadDsachTheoBB.IsChecked == true)
            {
                DS = 1;
                radIntheoNgay.IsChecked = true;
                radIntheoNgay.IsEnabled = true;
                radInChotNgay.IsEnabled = false;
                radInLai.IsEnabled = false;
                btnXoa.IsEnabled = false;
                get_IN();
            }
            if (RadDsachThuHo.IsChecked == true)
            {
                DS = 2;
                radIntheoNgay.IsChecked = true;
                radIntheoNgay.IsEnabled = true;
                radInChotNgay.IsEnabled = false;
                radInLai.IsEnabled = false;
                btnXoa.IsEnabled = false;
                get_IN();
            }
            if (RadDsachKhTonCuoithang.IsChecked == true)
            {
                DS = 3;
                //radIntheoNgay.IsChecked = false;
                radIntheoNgay.IsEnabled = false;
                //radInChotNgay.IsChecked = false;
                radInChotNgay.IsEnabled = false;
                radInLai.IsEnabled = false;
                dtptungay.IsEnabled = false;
                btnXoa.IsEnabled = false;
                dtpdenngay.IsEnabled = false;
                dtpNgayNop.IsEnabled = false;
            }
        }
        private void get_IN()
        {
            if (radIntheoNgay.IsChecked == true)
            {
                dtptungay.IsEnabled = true;
                dtpdenngay.IsEnabled = true;
                dtpNgayNop.IsEnabled = false;
                dgrThoiDiem.ItemsSource = null;
                IN = 0;
                btnXoa.IsEnabled = false;
                dtptungay.Focus();
            }
            if (radInChotNgay.IsChecked == true)
            {
                dtptungay.IsEnabled = false;
                dtpdenngay.IsEnabled = false;
                dtpNgayNop.IsEnabled = true;
                btnXoa.IsEnabled = false;
                IN = 1;
                dtpNgayNop.Focus();
            }
            if (radInLai.IsChecked == true)
            {
                dtptungay.IsEnabled = false;
                dtpdenngay.IsEnabled = false;
                dtpNgayNop.IsEnabled = true;
                IN = 2;
                dtpNgayNop.Focus();
            }
        }

        private void RadALLDSach_Click(object sender, RoutedEventArgs e)
        {
            get_DS();
        }

        private void RadDsachTheoBB_Click(object sender, RoutedEventArgs e)
        {
            get_DS();
        }

        private void RadDsachThuHo_Click(object sender, RoutedEventArgs e)
        {
            get_DS();
        }
        private void RadDsachKhTonCuoithang_Click(object sender, RoutedEventArgs e)
        {
            get_DS();
        }
        private void radIntheoNgay_Click(object sender, RoutedEventArgs e)
        {
            get_IN();
            dtptungay.Focus();
        }

        private void radInChotNgay_Click(object sender, RoutedEventArgs e)
        {
            get_IN();
            dtpNgayNop.Focus();
        }
        private void radInLai_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string strMatngan = strUsername;
                
                DateTime?  pngaythu;
                pngaythu = dtpNgayNop.SelectedDate;
                ngaynop = dtpNgayNop.SelectedDate;
                Int16 pthangIn = Convert.ToInt16(dtpThangHTtim.Text.Substring(0, 2)); ;
                Int16 pnamIn = Convert.ToInt16(dtpThangHTtim.Text.Substring(3, 4));
                DataSet ds = new DataSet();
                //ds = cls.select_lannoptien(strMadviqly, strMatngan, pthangIn, pnamIn,pngaythu, ref maloi);
                ds = GlobalServices.Service_NPCUS.select_lannoptien(strMadviqly, strMatngan, pthangIn, pnamIn, pngaythu, ref maloi);
                if (ds.Tables.Count != 0)
                {
                    if (ds.Tables[0].Rows.Count != 0)
                    {
                        dgrThoiDiem.ItemsSource = ds.Tables[0].DefaultView;
                        dgrThoiDiem.SelectedIndex = 0;
                    }
                    else
                    { dgrThoiDiem.ItemsSource = null; dgrThoiDiem.SelectedIndex = -1; }
                }
                else
                { dgrThoiDiem.ItemsSource = null; dgrThoiDiem.SelectedIndex = -1; }
                get_IN();
                dtpNgayNop.Focus();
            }
            catch
            { }
        }
        private void btnIn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string strMatngan = strUsername;
                DataSet ds_chamnooffline = new DataSet();
                DateTime? ptungay, pdenngay,pngaythu;
                ptungay = dtptungay.SelectedDate;
                pdenngay = dtpdenngay.SelectedDate;
                pngaythu = dtpNgayNop.SelectedDate;
                Int16 pthangIn = Convert.ToInt16(dtpThangHTtim.Text.Substring(0, 2)); ;
                Int16 pnamIn = Convert.ToInt16(dtpThangHTtim.Text.Substring(3, 4));
                string maloi ="";
                //int trangthai = 0;
                int ilsu = 0; //0: hien tai, 1: lich su
                if (pthangIn == i16ThangHT && pnamIn == i16NamHT)
                    ilsu = 0;
                else ilsu = 1;
                switch (IN)
                {
                    case 0:
                        ds_chamnooffline = GlobalServices.Service_NPCUS.get_dsthutien_offline(strMadviqly, strMatngan, pthangIn, pnamIn, ptungay, pdenngay, pngaythu, DS, IN,"");
                        //ds_chamnooffline = bus.get_dsthutien_offline(strMadviqly, strMatngan, pthangIn, pnamIn, ptungay, pdenngay, pngaythu, DS, IN, "");
                        if (ds_chamnooffline == null)
                        { showErrorMess("Không có dữ liệu theo điều kiện chọn"); return; }
                        if (ds_chamnooffline.Tables[0].Rows.Count == 0)
                        { showErrorMess("Không có dữ liệu theo điều kiện chọn"); return; }
                        if (radSo_GCS.IsChecked == true)
                        {
                            ds_chamnooffline.Tables[0].DefaultView.Sort = "MA_SOGCS,STT Asc";
                        }
                        if (rad_idcham.IsChecked == true)
                        {
                            ds_chamnooffline.Tables[0].DefaultView.Sort = "ID_OFFLINE Asc";
                        }

                        ds_chamnooffline.Tables[0].AcceptChanges();
                        //ds_chamnooffline = bus.get_dsthutien_offline(strMadviqly, strMatngan, pthangIn, pnamIn, ptungay, pdenngay, DS, IN);
                        
                        if (ds_chamnooffline.Tables["DSTHUTIEN"].Rows.Count > 0)
                        {
                            rptDsThutien_Offline rpt = new rptDsThutien_Offline();
                            rpt.SetDataSource(ds_chamnooffline.Tables["DSTHUTIEN"].DefaultView);
                            rpt.SetParameterValue("ptungay", ptungay);
                            rpt.SetParameterValue("pdenngay", pdenngay);
                            rpt.SetParameterValue("pTendienluc", strTen_dviqly);
                            rpt.SetParameterValue("pTencongty", strTen_dviqly_CT);
                            //ds_chamnooffline.WriteXmlSchema("D:\\Projects\\DSCHAMNO.xsd");
                            UtilReportOMS.RptManager.showReportToWin(this, rpt, strMadviqly, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");
                        }
                        else
                        { showErrorMess("Không có dữ liệu theo điều kiện chọn"); return; }
                        break;
                    case 1:
                        // thuc hien cap nhat so lieu chot
                        GlobalServices.Service_NPCUS.update_NPC_CHAMNO_OFFLINE_IN(strMadviqly, strMatngan, pthangIn, pnamIn, pngaythu,System.DateTime.Now.Date, ref tt, ref maloi);
                        //cls.update_NPC_CHAMNO_OFFLINE_IN(strMadviqly, strMatngan, pthangIn, pnamIn, pngaythu, System.DateTime.Now.Date, ref tt, ref maloi);
                        if (ds_chamnooffline == null)
                        { showErrorMess("Không có dữ liệu theo điều kiện chọn"); return; }
                        if (ds_chamnooffline.Tables[0].Rows.Count == 0)
                        { showErrorMess("Không có dữ liệu theo điều kiện chọn"); return; }
                        if (radSo_GCS.IsChecked == true)
                        {
                            ds_chamnooffline.Tables[0].DefaultView.Sort = "MA_SOGCS,STT Asc";
                        }
                        if (rad_idcham.IsChecked == true)
                        {
                            ds_chamnooffline.Tables[0].DefaultView.Sort = "ID_OFFLINE Asc";
                        }

                        ds_chamnooffline.Tables[0].AcceptChanges();
                        if (ds_chamnooffline.Tables["DSTHUTIEN"].Rows.Count > 0)
                        {
                            rptDsThutienChot_Offline rpt = new rptDsThutienChot_Offline();
                            rpt.SetDataSource(ds_chamnooffline.Tables["DSTHUTIEN"]);
                            rpt.SetParameterValue("ptungay", ptungay);
                            rpt.SetParameterValue("pdenngay", pdenngay);
                            rpt.SetParameterValue("lanchot", tt);
                            rpt.SetParameterValue("pTendienluc", strTen_dviqly);
                            rpt.SetParameterValue("pTencongty", strTen_dviqly_CT);
                            //ds_chamnooffline.WriteXmlSchema("D:\\Projects\\DSCHAMNO.xsd");
                            UtilReportOMS.RptManager.showReportToWin(this, rpt, strMadviqly, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");
                        }
                        else
                        { showErrorMess("Không có dữ liệu theo điều kiện chọn"); return; }
                        break;
                    case 2:
                        // in lại các phần chốt 
                        int rowindex = dgrThoiDiem.SelectedIndex;
                        if (rowindex < 0)
                        { showErrorMess("Chọn lần chốt để in"); return; }
                        DataRowView drv = dgrThoiDiem.SelectedItem as DataRowView;
                        string trangthai = drv.Row["STT"].ToString();
                        ds_chamnooffline = GlobalServices.Service_NPCUS.get_dsthutien_offline(strMadviqly, strMatngan, pthangIn, pnamIn, ptungay, pdenngay, ngaynop, DS, IN, trangthai);
                        //ds_chamnooffline = bus.get_dsthutien_offline(strMadviqly, strMatngan, pthangIn, pnamIn, ptungay, pdenngay, pngaythu, DS, IN, trangthai);

                        if (ds_chamnooffline == null)
                        { showErrorMess("Không có dữ liệu theo điều kiện chọn"); return; }
                        if (ds_chamnooffline.Tables[0].Rows.Count == 0)
                        { showErrorMess("Không có dữ liệu theo điều kiện chọn"); return; }
                        if (radSo_GCS.IsChecked == true)
                        {
                            ds_chamnooffline.Tables[0].DefaultView.Sort = "MA_SOGCS,STT Asc";
                        }
                        if (rad_idcham.IsChecked == true)
                        {
                            ds_chamnooffline.Tables[0].DefaultView.Sort = "ID_OFFLINE Asc";
                        }

                        ds_chamnooffline.Tables[0].AcceptChanges();
                        if (ds_chamnooffline.Tables["DSTHUTIEN"].Rows.Count > 0)
                        {
                            rptDsThutienChot_Offline rpt = new rptDsThutienChot_Offline();
                            rpt.SetDataSource(ds_chamnooffline.Tables["DSTHUTIEN"]);
                            rpt.SetParameterValue("ptungay", ptungay);
                            rpt.SetParameterValue("pdenngay", pdenngay);
                            rpt.SetParameterValue("lanchot", trangthai);
                            rpt.SetParameterValue("pTendienluc", strTen_dviqly);
                            rpt.SetParameterValue("pTencongty", strTen_dviqly_CT);
                            //ds_chamnooffline.WriteXmlSchema("D:\\Projects\\DSCHAMNO.xsd");
                            UtilReportOMS.RptManager.showReportToWin(this, rpt, strMadviqly, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");
                        }
                        else
                        { showErrorMess("Không có dữ liệu theo điều kiện chọn"); return; }
                        break;
                }
            }
            catch (Exception ex)
            { showErrorMess(ex.ToString()); }

        }

        private void dtpNgayNop_GotFocus(object sender, RoutedEventArgs e)
        {
            dtpNgayNop.Background = Brushes.Gold;
        }

        private void dtpNgayNop_LostFocus(object sender, RoutedEventArgs e)
        {
            dtpNgayNop.Background = Brushes.Transparent;
        }

        private void dtpThangHTtim_GotFocus(object sender, RoutedEventArgs e)
        {
            dtpThangHTtim.Background = Brushes.Gold;
        }

        private void dtpThangHTtim_LostFocus(object sender, RoutedEventArgs e)
        {
            dtpThangHTtim.Background = Brushes.Transparent;
        }

        private void dtptungay_GotFocus(object sender, RoutedEventArgs e)
        {
            dtptungay.Background = Brushes.Gold;
        }

        private void dtptungay_LostFocus(object sender, RoutedEventArgs e)
        {
            dtptungay.Background = Brushes.Transparent;
        }

        private void dtpdenngay_GotFocus(object sender, RoutedEventArgs e)
        {
            dtpdenngay.Background = Brushes.Gold;
        }

        private void dtpdenngay_LostFocus(object sender, RoutedEventArgs e)
        {
            dtpdenngay.Background = Brushes.Transparent;
        }

        private void dtpNgayNop_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (IN == 2)
                {
                    if (e.Key == Key.Enter)
                    {
                        string strMatngan = strUsername;
                        DateTime? pngaythu;
                        ngaynop = dtpNgayNop.SelectedDate;
                        pngaythu = dtpNgayNop.SelectedDate;
                        Int16 pthangIn = Convert.ToInt16(dtpThangHTtim.Text.Substring(0, 2)); ;
                        Int16 pnamIn = Convert.ToInt16(dtpThangHTtim.Text.Substring(3, 4));
                        DataSet ds = new DataSet();
                        //ds = cls.select_lannoptien(strMadviqly, strMatngan, pthangIn, pnamIn, pngaythu, ref maloi);
                        ds = GlobalServices.Service_NPCUS.select_lannoptien(strMadviqly, strMatngan, pthangIn, pnamIn, pngaythu, ref maloi);
                        if (ds.Tables.Count != 0)
                        {
                            if (ds.Tables[0].Rows.Count != 0)
                            {
                                dgrThoiDiem.ItemsSource = ds.Tables[0].DefaultView;
                            }
                            else
                            { dgrThoiDiem.ItemsSource = null; }
                        }
                        else
                        { dgrThoiDiem.ItemsSource = null; }
                    }
                }

            }
            catch
            { }
        }

        private void dgrThoiDiem_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                int index = dgrThoiDiem.SelectedIndex;
                int DongCuoi = dgrThoiDiem.Items.Count - 1;
                //DataTable dt = dsResultNCS.Tables[0] as DataTable;
                //DataRowView drg = dgr_nhapchiso.SelectedItem as DataRowView;
                //DataRow[] dr = dt.Select();
                if (index < 0)
                {
                    
                }
                else
                {
                    if (radInLai.IsChecked == true)
                    {
                        btnXoa.IsEnabled = true;
                    }
                    else
                    { btnXoa.IsEnabled = false; }
                }
            }
            catch
            { }
        }

        private void btnXoa_Click(object sender, RoutedEventArgs e)
        {
            string strMatngan = strUsername;
            Int16 pthangIn = Convert.ToInt16(dtpThangHTtim.Text.Substring(0, 2)); ;
            Int16 pnamIn = Convert.ToInt16(dtpThangHTtim.Text.Substring(3, 4));
            int rowindex = dgrThoiDiem.SelectedIndex;
            int DongCuoi = dgrThoiDiem.Items.Count;
            if (rowindex < 0)
            { showErrorMess("Chọn lần chốt để in"); return; }
            DataRowView drv = dgrThoiDiem.SelectedItem as DataRowView;
            int trangthai = Convert.ToInt32(drv.Row["STT"]);
            if (MessageBox.Show("Bạn muốn xóa chốt lần :" + trangthai.ToString() + "! \r\n Nhấn Yes : Thực hiện xóa.\r\n Nhấn No  : Không thực hiện.", "Thông báo", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
            {
                //if (!cls.update_NPC_CHAMNO_OFFLINE_XOA(strMadviqly, strMatngan, pthangIn, pnamIn, ngaynop, trangthai, DongCuoi, ref maloi))
                if (!GlobalServices.Service_NPCUS.update_NPC_CHAMNO_OFFLINE_XOA(strMadviqly, strMatngan, pthangIn, pnamIn, ngaynop, trangthai, DongCuoi, ref maloi))
                {
                    showErrorMess(maloi);
                    return;
                }
            }
            showSucrMess("Xóa lần chốt số liệu thành công!");
            DataSet ds = new DataSet();
            //ds = cls.select_lannoptien(strMadviqly, strMatngan, pthangIn, pnamIn,pngaythu, ref maloi);
            ds = GlobalServices.Service_NPCUS.select_lannoptien(strMadviqly, strMatngan, pthangIn, pnamIn, ngaynop, ref maloi);
            if (ds.Tables.Count != 0)
            {
                if (ds.Tables[0].Rows.Count != 0)
                {
                    dgrThoiDiem.ItemsSource = ds.Tables[0].DefaultView;
                    dgrThoiDiem.SelectedIndex = 0;
                }
                else
                { dgrThoiDiem.ItemsSource = null; dgrThoiDiem.SelectedIndex = -1; }
            }
            else
            { dgrThoiDiem.ItemsSource = null; dgrThoiDiem.SelectedIndex = -1; }
            get_IN();
            dtpNgayNop.Focus();
        }
    }
}
